<?php 
/*
 module:		Mbti订单表
 create_time:	2024-04-22 17:31:27
 author:		
 contact:		
*/

namespace app\api\controller\Mbti;

use app\api\service\Mbti\OrderService;
use app\api\model\Mbti\Order as OrderModel;
use app\api\controller\Common;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;

class Order extends Common {




 /**start**/
	/**
	* @api {get} /Mbti.Order/getTopic 02、获取订单题目
	* @apiGroup Order
	* @apiVersion 1.0.0
	* @apiDescription  获取订单题目
	
	* @apiParam (输入参数：) {string}     		order_id 主键ID

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码 201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.data 返回数据详情
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":""}
	* @apiErrorExample {json} 02 失败示例
	* {"status":"201","msg":"没有数据"}
	*/
	function getTopic(){
		$data['order_son'] = $this->request->post('order_son','','serach_in');

		$res  = checkData(OrderService::getTopic($data));
		return $this->ajaxReturn($this->successCode,'返回成功',$res);
	}


	function save(){
		$postField = 'mbti_id';
        $data = $this->request->only(explode(',',$postField),'post',null);
//        var_dump($data);die;
        $uid = $this->getUidByToken($this->request);
		$res = OrderService::save($data,$uid);
		return $this->ajaxReturn($this->successCode,'操作成功',$res);
	}

    /**
     * //订单结果
     * @return false|string
     */
    public function resultAnswer(){
        $postField = 'order_son';
        $data = $this->request->only(explode(',',$postField),'post',null);
//        var_dump($data);die;
        $res = checkData(OrderService::result_answer($data));

        return $this->ajaxReturn($this->successCode,'操作成功',$res);
    }
    /**
     * //订单支付页
     * @return false|string
     */
    public function orderPayment(){
        $postField = 'order_son';
        $data = $this->request->only(explode(',',$postField),'post',null);
//        var_dump($data);die;
        $res = checkData(OrderService::order_payment($data));

        return $this->ajaxReturn($this->successCode,'操作成功',$res);
    }

    /**
     * //订单支付页
     * @return false|string
     */
    function subResult(){

        $postField = 'order_son,data,second';
        $data = $this->request->only(explode(',',$postField),'post',null);
        if(empty($data['order_son'])){
            throw new ValidateException('参数错误');
        }
        $res  = checkData(OrderService::subResult($data));
        return $this->ajaxReturn($this->successCode,'操作成功',$res);
    }

    /**
     * 订单查询
     * @return false|string
     */

    function getPaymentState(){
        $postField = 'order_son';
        $data = $this->request->only(explode(',',$postField),'post',null);
        if(empty($data['order_son'])){
            throw new ValidateException('请输入订单号');
        }
        $where['order_son'] = $data['order_son'];
        try{
            $res = OrderModel::where($where)->field('state')->find();
            if(empty($res)){
                $res["state"] = 2;
            }
        }catch(\Exception $e){
            abort(config('my.error_log_code'),$e->getMessage());
        }
        return $this->ajaxReturn($this->successCode,'操作成功',$res);
    }

    /**
     * //订单微信
     * @return false|string
     */
    public function orderWxPay(){
        $postField = 'order_son,type';
        $data = $this->request->only(explode(',',$postField),'param',null);
        $uid = $this->getUidByToken($this->request);
        $res = checkData(OrderService::order_wx_pay($data,$uid));
        return $this->ajaxReturn($this->successCode,'操作成功',$res);
    }

    /***
     * 订单绑定给当前用户
     */
    public function orderBinding()
    {
        $postField = 'order_son';
        $data = $this->request->only(explode(',',$postField),'param',null);
        if(empty($data['order_son'])){
            throw new ValidateException('参数错误');
        }
        $uid = $this->getUidByToken($this->request);
        $order = OrderModel::where("order_son",$data["order_son"])->find();
        if(empty($order["uid"])){
            OrderModel::where("order_id",$order["order_id"])->update(["uid"=>$uid]);
        }
        return $this->ajaxReturn($this->successCode,'操作成功');
    }


    /**end**/



}

